package com.gip.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.gip.dto.ResgisterInfo;
import com.gip.dto.UserInfo;
import com.gip.mapper.UserMapper;


import com.gip.pojo.Auction;
import com.gip.pojo.User;
import com.gip.service.AuctionService;
import com.gip.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
   private UserService userService;
    @Autowired
    private AuctionService auctionService;

    @RequestMapping("/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        //CircleCaptcha captcha = new CircleCaptcha(200, 100, 4, 20);
        //图形验证码写出，可以写出到文件，也可以写出到流
        ServletOutputStream outputStream = response.getOutputStream();
        captcha.write(outputStream);
        outputStream.close();
        session.setAttribute("CODE", captcha.getCode());
        session.setAttribute("captcha", captcha);
    }

    @RequestMapping("/toLogin")
    public String toLogin(UserInfo userInfo, HttpSession session, Model model) {
        System.out.println(userInfo);
        String code1 = (String) session.getAttribute("CODE");
        String code = userInfo.getCode();
        System.out.println(code);
        if(code1==null){
            return "login";
        }
        if (code == null || !code1.equals(code)) {
            model.addAttribute("msg", "验证码不正确");
            return "login";
        }
        List<User> login = userService.isLogin(userInfo.getUserName(), userInfo.getPassword());
        if(login!=null&&login.size()!=0){
            User user = login.get(0);
            session.setAttribute("user",user);
//            List<Auction> auctions = auctionService.queryAllAuctions();
//            model.addAttribute("auctionList",auctions);
//            return "index";
            return "redirect:/queryAllAuctions";
        }

        model.addAttribute("msg","用户名或者密码错误");
        return "login";
    }

    @RequestMapping("/")
    public String login() {
        return "login";
    }

    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    @RequestMapping("/toRegister")
    public String toRegister(ResgisterInfo resgisterInfo, HttpSession session, Model model) {
        String code1 = resgisterInfo.getCode();
        String code = (String) session.getAttribute("CODE");
        if(code==null){
            return "register";
        }
        if (code1 == null || !code.equals(code1)) {
            model.addAttribute("msg", "验证码不正确");
            return "register";
        }
        int i = userService.registerUser(resgisterInfo);
        if ( i >= 1) {
                //   注册成功，
                model.addAttribute("msg", "注册成功");
                return "register";
            }else{
                model.addAttribute("msg", "注册失败");
                return "register";
            }
    }


    @RequestMapping("/forget")
    public String forget(){
        return "forget";
    }
    @RequestMapping("/toForget")
    public String toForget(String userName,String tel , String password,String code,HttpSession session,Model model){
        String code1 = (String) session.getAttribute("CODE");
        if(code1==null){
            return "forget";
        }
        if (code == null || !code.equals(code1)) {
            model.addAttribute("msg", "验证码不正确");
            return "forget";
        }
        int i = userService.updateUser(userName, tel, password);
        if(i>=1){
            model.addAttribute("msg","修改成功");
            return "forget";
        }else{
            model.addAttribute("msg","修改成功");
            return "forget";
        }


//        int i = userService.registerUser(resgisterInfo);
//        if ( i >= 1) {
//            //   注册成功，
//            model.addAttribute("msg", "注册成功");
//            return "register";
//        }else{
//            model.addAttribute("msg", "注册失败");
//            return "register";
//        }

    }

    @RequestMapping("layout")
    public String layout(HttpSession session){
        session.invalidate();
        return "redirect:/";
    }

//    @RequestMapping("/getCode")
//    public void getCode(HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws Exception {
//        Code.drawCode(httpServletResponse, httpServletRequest);
//    }
}
